<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="部门名称" prop="deptName">
        <el-input v-model="queryParams.deptName" placeholder="请输入部门名称" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="员工姓名" prop="employeeName">
        <el-input v-model="queryParams.employeeName" placeholder="请输入员工姓名" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>



      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8" v-if="showControls">
      <el-col :span="1.5">
        <el-button type="primary" icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="Edit" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">

      <!-- 平台 -->
      <template v-if="route.meta.title == '日常检查'">
        <el-table-column label="序号" align="center" type="index" width="55">
          <template #default="{ $index }">
            {{ (queryParams.currentPage - 1) * queryParams.pageSize + $index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="员工姓名" align="center" prop="employeeName" />
        <el-table-column label="员工照片" width="100" align="center" prop="employeeAvatar">
          <template #default="scope">
            <el-image style="width: 50px; height: 50px;" :src="scope.row.employeeAvatar"
              :preview-src-list="[scope.row.employeeAvatar]" preview-teleported>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="检查项目总汇" width="150" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleDetail(scope.row)">检查项目总汇</el-button>
          </template>
        </el-table-column>
        <el-table-column label="预警设置" align="center" prop="remindSetting" />
        <el-table-column label="漏检报警" align="center" prop="forgetRemind	" />
        <el-table-column label="处理意见" align="center" prop="smsRemind" />
        <el-table-column label="所属行业" align="center" prop="industryName" />
        <el-table-column label="所属系统" align="center" prop="systemName" />
        <el-table-column label="所属单位" align="center" prop="merchantName" />
        <el-table-column label="单位地址" align="center" prop="addressDetail" />
        <el-table-column label="单位电话" align="center" prop="officeSpacePhone" />
        <el-table-column label="检查日期" align="center" prop="date" />
        <el-table-column label="状态" align="center" prop="checkStatus" width="100">
          <template #default="scope">
            <dict-tag :options="[
              {
                label: '待检查',
                value: '0'
              },
              {
                label: '合格',
                value: '1'
              },
              {
                label: '不合格',
                value: '2'
              }
            ]" :value="Number(scope.row.checkStatus)" />
          </template>
        </el-table-column>
        <el-table-column label="操作人员" align="center" prop="updateUserName" />
      </template>
      <!-- 单位 -->
      <template v-if="route.meta.title == '日常事务'">
        <el-table-column label="序号" align="center" type="index" width="55">
          <template #default="{ $index }">
            {{ (queryParams.currentPage - 1) * queryParams.pageSize + $index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="所属部门" align="center" prop="deptName" />
        <el-table-column label="员工姓名" align="center" prop="employeeName" />
        <el-table-column label="员工头像" width="100" align="center" prop="employeeAvatar">
          <template #default="scope">
            <el-image style="width: 50px; height: 50px;" :src="scope.row.employeeAvatar"
              :preview-src-list="[scope.row.employeeAvatar]" preview-teleported>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="检查项目总汇" width="150" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleDetail(scope.row)">检查项目总汇</el-button>
          </template>
        </el-table-column>
        <el-table-column label="预警设置" align="center" prop="remindSetting" />
        <el-table-column label="漏检报警" align="center" prop="forgetRemind	" />
        <el-table-column label="处理意见" align="center" prop="smsRemind" />
        <el-table-column label="检查日期" align="center" prop="date" />
        <el-table-column label="状态" align="center" prop="checkStatus" width="100">
          <template #default="scope">
            <dict-tag :options="[
              {
                label: '待检查',
                value: '0'
              },
              {
                label: '合格',
                value: '1'
              },
              {
                label: '不合格',
                value: '2'
              }
            ]" :value="Number(scope.row.checkStatus)" />
          </template>
        </el-table-column>
        <el-table-column label="操作人员" align="center" prop="updateUserName" />
      </template>

    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.currentPage"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改事务对话框 -->
    <el-dialog :title="title" v-model="open" width="780px" append-to-body>
      <el-form ref="noticeRef" :model="form" disabled :rules="rules" label-width="140px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="商务规范检查" prop="checkBusiness">
              <el-select v-model="form.checkBusiness">
                <el-option label="待检查" :value="0"></el-option>
                <el-option label="合格" :value="1"></el-option>
                <el-option label="不合格" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="流调普查" prop="checkCode">
              <el-select v-model="form.checkCode">
                <el-option label="待检查" :value="0"></el-option>
                <el-option label="合格" :value="1"></el-option>
                <el-option label="不合格" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="门前规范检查" prop="checkDoor">
              <el-select v-model="form.checkDoor">
                <el-option label="待检查" :value="0"></el-option>
                <el-option label="合格" :value="1"></el-option>
                <el-option label="不合格" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="用电安全检查" prop="checkElectricity">
              <el-select v-model="form.checkElectricity">
                <el-option label="待检查" :value="0"></el-option>
                <el-option label="合格" :value="1"></el-option>
                <el-option label="不合格" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="疫情规范检查" prop="checkEpidemic">
              <el-select v-model="form.checkEpidemic">
                <el-option label="待检查" :value="0"></el-option>
                <el-option label="合格" :value="1"></el-option>
                <el-option label="不合格" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="消防安全检查" prop="checkFire">
              <el-select v-model="form.checkFire">
                <el-option label="待检查" :value="0"></el-option>
                <el-option label="合格" :value="1"></el-option>
                <el-option label="不合格" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="环境卫生检查" prop="checkHygiene">
              <el-select v-model="form.checkHygiene">
                <el-option label="待检查" :value="0"></el-option>
                <el-option label="合格" :value="1"></el-option>
                <el-option label="不合格" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="食药规范检查" prop="checkMedicine">
              <el-select v-model="form.checkMedicine">
                <el-option label="待检查" :value="0"></el-option>
                <el-option label="合格" :value="1"></el-option>
                <el-option label="不合格" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="防抢安全检查" prop="checkRob">
              <el-select v-model="form.checkRob">
                <el-option label="待检查" :value="0"></el-option>
                <el-option label="合格" :value="1"></el-option>
                <el-option label="不合格" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="医疗废弃物规范检查" prop="checkRubbish">
              <el-select v-model="form.checkRubbish">
                <el-option label="待检查" :value="0"></el-option>
                <el-option label="合格" :value="1"></el-option>
                <el-option label="不合格" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="法规学习检查" prop="checkRuleStudy">
              <el-select v-model="form.checkRuleStudy">
                <el-option label="待检查" :value="0"></el-option>
                <el-option label="合格" :value="1"></el-option>
                <el-option label="不合格" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="业务学习检查" prop="checkServiceStudy">
              <el-select v-model="form.checkServiceStudy">
                <el-option label="待检查" :value="0"></el-option>
                <el-option label="合格" :value="1"></el-option>
                <el-option label="不合格" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="防盗安全检查" prop="checkSteal">
              <el-select v-model="form.checkSteal">
                <el-option label="待检查" :value="0"></el-option>
                <el-option label="合格" :value="1"></el-option>
                <el-option label="不合格" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="纳税规范检查" prop="checkTax">
              <el-select v-model="form.checkTax">
                <el-option label="待检查" :value="0"></el-option>
                <el-option label="合格" :value="1"></el-option>
                <el-option label="不合格" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup name="Notice">
import {
  authApiIndustryList
} from "@/api/login.js";

const industryList = ref([])
function authApiIndustryListApi() {
  authApiIndustryList().then(
    (res) => {
      industryList.value = [...res];
    }
  );
}
onMounted(() => {
  authApiIndustryListApi()
})


import {
  shiftApichecklistmerchant,
  shiftApichecklist,
  shiftApiCheckDetail,
  getNotice,
  delNotice,
  addNotice,
  updateNotice,
} from "@/api/backShift/backShift";

const route = useRoute()
const showControls = ref(false)
if (route.meta.title == '日常事务') {
  console.log('单位');

  showControls.value = true
} else if (route.meta.title == '日常检查') {
  console.log('平台');
  showControls.value = false

}

const options = [
  /* 0 待检查 1 合格 2 不合格 */
  {
    label: '待检查',
    value: 0
  },
  {
    label: '合格',
    value: 1
  },
  {
    label: '不合格',
    value: 2
  }
]



const { proxy } = getCurrentInstance();
const { check_status } = proxy.useDict(
  "check_status"
);

const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    currentPage: 1,
    pageSize: 10,
    noticeTitle: undefined,
    createBy: undefined,
    status: undefined,
  },
  rules: {
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询事务列表 */
function getList() {
  loading.value = true;

  if (route.meta.title == '日常事务') {
    // console.log('单位');
    shiftApichecklistmerchant(queryParams.value).then((response) => {
      noticeList.value = response.records;
      total.value = response.total;
      loading.value = false;
    });
  } else if (route.meta.title == '日常检查') {
    // console.log('平台');
    shiftApichecklist(queryParams.value).then((response) => {
      noticeList.value = response.records;
      total.value = response.total;
      loading.value = false;
    });
  }


}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    "checkHygiene": 0,
    "checkElectricity": 0,
    "checkFire": 0,
    "checkSteal": 0,
    "checkRob": 0,
    "checkDoor": 0,
    "checkMedicine": 0,
    "checkEpidemic": 0,
    "checkRubbish": 0,
    "checkBusiness": 0,
    "checkTax": 0,
    "checkServiceStudy": 0,
    "checkRuleStudy": 0,
    "checkCode": 0
  };
  proxy.resetForm("noticeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.currentPage = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.noticeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加事务";
}


/**修改按钮操作 */
function handleDetail(row) {
  reset();
  const employeeClockId = row.employeeClockId || ids.value;
  shiftApiCheckDetail(employeeClockId).then((response) => {
    form.value = { ...form.value, ...response };
    open.value = true;
    title.value = "检查详情";
  });
}


function handleUpdate(row) {
  reset();
  const noticeId = row.noticeId || ids.value;
  getNotice(noticeId).then((response) => {
    form.value = response;
    open.value = true;
    title.value = "修改事务";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate((valid) => {
    if (valid) {
      if (form.value.noticeId != undefined) {
        updateNotice(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addNotice(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const noticeIds = row.noticeId || ids.value;
  proxy.$modal
    .confirm('是否确认删除事务编号为"' + noticeIds + '"的数据项？')
    .then(function () {
      return delNotice(noticeIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}

getList();
</script>